package com.example.loginjwtdemo.controller;

import com.example.loginjwtdemo.pojo.User;
import com.example.loginjwtdemo.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
public class LoginController {
    @PostMapping("/login")
    public String login(@RequestBody User user, HttpSession session){
        if ("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())) {
//            session.setAttribute("user",user);
            String generate = JwtUtil.generate(user.getUsername());
            return generate;
        }
        return null;
    }

    @GetMapping("/logout")
    public boolean logout(HttpSession session){
        session.removeAttribute("user");
        return true;
    }





}
